﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Input;
using KharaSoft.ProjectManager.Core;
using System.Collections.ObjectModel;

namespace KharaSoft.ProjectManager.Windows
{
    public class ProjectDetailsViewModel:INotifyPropertyChanged
    {
        private Project _project;
        private readonly EventAggregator _eventAggregator;
        private readonly Action<Project> _projectChangedHandler;
        private readonly ICommand _addWorkItemCommand;


        public ProjectDetailsViewModel()
        {
            _eventAggregator = new EventAggregator();
            _projectChangedHandler = new Action<Project>(ChangeProject);
            _eventAggregator.RegisterForEvent(_projectChangedHandler);
            _addWorkItemCommand = new DelegateCommand(null, AddTask);
        }

        public ICommand AddWorkItemCommand
        {
            get { return _addWorkItemCommand; }
        }

        private void AddTask(object obj)
        {
            Project.Tasks.Add(new Task {Name = "Some New Task" + DateTime.Now});
        }

        private IEnumerable<Task> _projectTasks;
        public IEnumerable<Task> ProjectTasks
        {
            get { return _projectTasks; }
            private set
            {
                _projectTasks = value;
                PropertyChanged(this, new PropertyChangedEventArgs("ProjectTasks"));
            }
        }

        private void ChangeProject(Project project)
        {
            Project = project;
            ProjectTasks = new ObservableCollection<Task>(project.Tasks);
        }

        public Project Project
        {
            get { return _project; }
            set
            {
                _project = value;
                PropertyChanged(this, new PropertyChangedEventArgs("Project"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
